package com.cat.business.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;

import com.abigdreamer.arkflow.ProjectConstants;
import com.abigdreamer.arkflow.WorkflowDefineAnalyzer;
import com.abigdreamer.arkflow.WorkflowJsonToXml;
import com.abigdreamer.arkflow.WorkflowPath;
import com.abigdreamer.arkflow.WorkflowNode;
import com.abigdreamer.arkflow.jpdl.JpdlModel;
import com.abigdreamer.arkflow.jpdl.JpdlModelDrawer;
import com.cat.business.BizWorkFlowBusinessService;
import com.cat.entity.BizWorkflow;
import com.cat.service.common.BizWorkFlowService;
import com.cat.util.FileUtil;
import com.cat.util.TwoTuple;
import com.cat.util.ZipTools;

/**
 * @ClassName: UserManageBusinessServiceImpl
 * @Description: 人员管理
 * @author Jiangcl
 * @date 2012-2-16下午01:24:48
 * 
 */
@Service
public class BizWorkFlowBusinessServiceImpl implements BizWorkFlowBusinessService {

	@Resource
	private BizWorkFlowService workflowService;

	@Override
	public BizWorkflow getByDeploymentId(Integer deploymentId) {
		return workflowService.getByDeploymentId(deploymentId);
	}

	@Override
	public void delete(BizWorkflow entity) {
		workflowService.delete(entity);

	}

	@Override
	public Boolean saveWork(BizWorkflow entity) {
		try {
			if (entity.getWorkflowId() == null) {
				workflowService.save(entity);
			} else {
				workflowService.update(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public TwoTuple<List<WorkflowNode>, WorkflowNode> getJson(String jsonStr) {

		return new WorkflowDefineAnalyzer().execute(jsonStr);
	}

	public String JsonToXml(List<WorkflowNode> states, HashMap<String, String> idNameMap, WorkflowNode workflow) {
		return new WorkflowJsonToXml().toXml(states, idNameMap, workflow);
	}

	@Override
	public String addWorkflow(List<WorkflowNode> states, HashMap<String, String> idNameMap, WorkflowNode workflow) throws Exception {
		// 1.根据对象，拼凑xml文件内容
		String xml = JsonToXml(states, idNameMap, workflow);
		// 2.保存xml文件
		String xmlPath = ProjectConstants.workflowFilePath + workflow.getName() + ".jpdl.xml";
		String imagePath = ProjectConstants.workflowFilePath + workflow.getName() + ".png";
		String zipPath = ProjectConstants.workflowFilePath + workflow.getName() + ".zip";
		if (FileUtil.fileWrite(xml, xmlPath)) {
			// 3.生成png图片
			try {
				File file = new File(xmlPath);
				InputStream is = new FileInputStream(file);
				JpdlModel jpdlModel = new JpdlModel(is);
				ImageIO.write(new JpdlModelDrawer().draw(jpdlModel), "png", new File(imagePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 4.生成zip包
			List<String> fileNameList = new ArrayList<String>();
			fileNameList.add(xmlPath);
			fileNameList.add(imagePath);
			try {
				ZipTools.zipFile(ProjectConstants.workflowFilePath, fileNameList, zipPath);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return zipPath;
		}
		return "";
	}

	@Override
	public List<BizWorkflow> getByWorkflowName(String workflowName) {
		return workflowService.getByWorkflowName(workflowName);
	}
}
